import 'package:flutter/cupertino.dart';
import 'package:ride_rex/constant.dart';
import 'package:ride_rex/local_data/local_data_utils.dart';
import 'package:ride_rex/net/common_global_url.dart';
import 'package:ride_rex/net/common_net.dart';
import 'package:ride_rex/route.dart';
import 'package:ride_rex/ui/common_dialogs.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../../login/login_logic.dart';
import '../../../service/log_service.dart';

class ApiSetting extends CommonApi {
  ApiSetting.deleteAccount() : super(CommonGlobalUrl.deleteAccount);
}

class SettingLogic with OnceClick {
  final ValueNotifier<bool> autoTranslate = ValueNotifier(false);

  void init() {
    autoTranslate.value = CommonStorage().getBool(StorageKey.spKeyAutoTranslate);
  }

  Future logout() async {
    LMLogManager.stat(page: 'logout');
    try {
      startOnce(() async {
        await LogoutLogic.logout();
        CommonRoute.openLogin();
        Loading.dismiss();
      });
    } catch (e) {
      Loading.toast(e.toString());
    }
  }

  Future deleteAccount() async {
    LMLogManager.stat(page: 'deleteaccount');
    CommonDialogs.twoButton(
        title: 'Delete Account',
        description: 'Are you sure you want to delete your account?',
        cancelTitle: 'Cancel',
        confirmTitle: 'Delete',
        onConfirm: () async {
          try {
            startOnce(() async {
              await ApiSetting.deleteAccount().post();
              MyPostArticleUtils.clear();
              LogoutLogic.clear();
              Loading.toast('Delete success');
              CommonRoute.openLogin();
            });
          } catch (e) {
            Loading.toast(e.toString());
          }
        });
  }

  void onTaoAutoTranslate() async {
    autoTranslate.value = !autoTranslate.value;
    await CommonStorage().setBool(StorageKey.spKeyAutoTranslate, autoTranslate.value);
  }
}
